package com.sojson.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import com.sojson.util.shutdown.CustomMethodShutdown;

/**
 * 系统工具类
 * 
 * @author liu
 * @date 2021-04-28
 */
public class SystemUtil {

    public static final List<CustomMethodShutdown> THREAD_SHUTDOWN = new ArrayList<>();
    /** 因为连接需要时间,所以需要在连接的时候暂停线程,连接成功后会有个回调方法,在回调方法中让计数减一后变成0恢复线程 */
    public static CountDownLatch THREAD_SHUTDOWN_COUNT = new CountDownLatch(0);

    /**
     * 添加程序退出时执行的任务
     * 
     * @param thread
     */
    public static void addShutdown(CustomMethodShutdown method) {
        THREAD_SHUTDOWN.add(method);
        synchronized (SystemUtil.class) {
            THREAD_SHUTDOWN_COUNT = new CountDownLatch((int)THREAD_SHUTDOWN_COUNT.getCount() + 1);
        }
    }

    /**
     * 退出系统
     */
    public static void exit() {
        System.exit(0);
    }

}